package interfaces;

import java.util.Hashtable;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.Vector;
import javax.swing.*;


import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JSlider;
import javax.swing.JSpinner;
import javax.swing.JTextField;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.plaf.ColorUIResource;

import dinnerSimulation.Action;
import dinnerSimulation.Coord;
import dinnerSimulation.Environment;
import dinnerSimulation.Floor;
import dinnerSimulation.Square;
import dinnerSimulation.Environment.Type;

import java.util.Timer;

public class SettingsPanel extends JPanel
{

    private static final long serialVersionUID = 1L;

    public MainJFrame mainFrame;

    private JPanel dimensionPanel;
    private JSpinner sizeField;
    private JLabel sizeLabel;
    private JLabel room1;
    private JLabel room2;
    private JLabel room3;
    private JLabel room4;
    private JComboBox roomTypeCombobox1;
    private JComboBox roomTypeCombobox2;
    private JComboBox roomTypeCombobox3;
    private JComboBox roomTypeCombobox4;

    private JPanel generationPanel;
    private JSpinner stepField;
    private JLabel stepLabel;
    private JSpinner arrivalTimeField;
    int time_limit=0;
    private JLabel arrivalTimeLabel;
    public int selectedRoom =0;
    int speed =1;

    private JSlider jSlider;
    private JLabel jSliderLabel;

    private JPanel commandPanel;
    private JPanel actionPanel;
    private JButton refreshButton;
    private JButton randomButton;
    JButton controlButton, resetButton;
    Timer t;
    int n;
    private JLabel progressBarLabel;
    private JProgressBar jProgressBar1;

    private JPanel statusPanel;
    private JLabel pmLabel;
    private JLabel pmValueLabel;
    private JLabel stepsLabel;
    private JLabel stepsValueLabel;
    private JLabel subGoalsLabel;
    private JLabel subGoal1Label;
    private JLabel subGoal2Label;
    private JLabel subGoal3Label;
    private JLabel subGoal4Label;
    private JLabel subGoal5Label;

    private int max_dim = 8;
    private int min_dim = 4;

    private JLabel envTypeLabel;
    private JComboBox envTypeCombobox;

    ImageIcon playIcon, stopIcon, nextIcon;

    private JButton onesButton;
    private JLabel currTypeEnvLabel;
    private JLabel currTypeEnvValueLabel;


    private JLabel goalReachedLabel;
    final Vector<Floor.roomType> floorTypeVector = new Vector<Floor.roomType>();

    private JLabel kLabel;

    private JSpinner kField;

    private boolean generated = false;
    /**
     *
     * @param mainFrame
     */
    @SuppressWarnings("deprecation")
    public SettingsPanel(final MainJFrame mainFrame)
    {
        {
            this.mainFrame = mainFrame;

            playIcon = ImageLoader.playIcon;
            stopIcon = ImageLoader.stopIcon;
            nextIcon = ImageLoader.nextIcon;

            GridBagLayout jPanel2Layout = new GridBagLayout();
            jPanel2Layout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
            jPanel2Layout.rowHeights = new int[] {1,1,1,1};
            jPanel2Layout.columnWeights = new double[] {0.1};
            jPanel2Layout.columnWidths = new int[] {1};

            setLayout(jPanel2Layout);
            
            {
                /*setting input fields*/
                generationPanel = new JPanel();
                generationPanel.setPreferredSize(new Dimension(300,100));
                Border marginOutside = new EmptyBorder(1,10,1,10);
                TitledBorder title = BorderFactory.createTitledBorder("Build Settings");
                CompoundBorder upperBorder = new CompoundBorder(marginOutside, title);
                Border marginInside = new EmptyBorder(2,10,2,10);
                generationPanel.setBorder(new CompoundBorder(upperBorder, marginInside));
                generationPanel.setLayout(new GridLayout(4,1));
                add(generationPanel, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                envTypeLabel = new JLabel("Enviroment Type");
                Vector<Environment.Type> envTypeVector = new Vector<Environment.Type>();
                envTypeVector.add(Environment.Type.DYNAMIC_1);
                envTypeVector.add(Environment.Type.DYNAMIC_2);
                envTypeVector.add(Environment.Type.DYNAMIC_3);
                envTypeVector.add(Environment.Type.STATIC);

                envTypeCombobox = new JComboBox(envTypeVector);
                envTypeCombobox.setSelectedItem(mainFrame.env.type);
                envTypeCombobox.addItemListener(new ItemListener()
                {

                    @Override
                    public void itemStateChanged(ItemEvent arg0)
                    {
                        mainFrame.env.type = (Type) envTypeCombobox.getSelectedItem();
                        if(envTypeCombobox.getSelectedIndex()==3)
                            kField.setEnabled(false);
                        else
                            kField.setEnabled(true);
                    //   refreshButton.setBackground(Color.YELLOW);
                        update();
                    }





                });

                generationPanel.add(envTypeLabel);
                generationPanel.add(envTypeCombobox);

                /*number of steps*/
                stepLabel = new JLabel();
                stepLabel.setText("Step");
                stepField = new JSpinner();
                stepField.setValue(50);
                stepField.setPreferredSize(new Dimension(40, 30));
                stepField.addChangeListener(new ChangeListener()
                {



                    @Override
                    public void stateChanged(ChangeEvent arg0)
                    {
                    	//refreshButton.setBackground(Color.YELLOW);
                        System.out.println("stepField changed value = " + stepField.getValue());
                        
                    }
                });

                generationPanel.add(stepLabel);
                generationPanel.add(stepField);

              /*  refreshButton = new JButton();
              
                refreshButton.setText("Refresh");
                refreshButton.setPreferredSize(new Dimension(75,40));*/
                /*k */
                kLabel = new JLabel();
                kLabel.setText("K");
                kField = new JSpinner();
                kField.setValue(5);
                //kField.enableInputMethods(false);
                mainFrame.env.agent.setKValue((Integer) kField.getValue());
                kField.setPreferredSize(new Dimension(40, 30));
                kField.addChangeListener(new ChangeListener()
                {



                    @Override
                    public void stateChanged(ChangeEvent arg0)
                    {
                      //  refreshButton.setBackground(Color.YELLOW);
                        System.out.println("kField changed value = " + kField.getValue());
                        mainFrame.env.agent.setKValue((Integer)kField.getValue());
                    }
                });

                generationPanel.add(kLabel);
                generationPanel.add(kField);
             //   generationPanel.add(refreshButton);





            }

            {

                dimensionPanel = new JPanel();
                dimensionPanel.setPreferredSize(new Dimension(300,170));

                Border marginOutside = new EmptyBorder(10,10,10,10);
                TitledBorder title = BorderFactory.createTitledBorder("Room size");
                CompoundBorder upperBorder = new CompoundBorder(marginOutside, title);
                Border marginInside = new EmptyBorder(10,10,10,10);
                dimensionPanel.setBorder(new CompoundBorder(upperBorder, marginInside));
                /*input field to set the size of the floor*/
                dimensionPanel.setLayout(new GridLayout(6,2));
                add(dimensionPanel, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                sizeLabel = new JLabel();
                dimensionPanel.add(sizeLabel);
                sizeLabel.setText("Size");
                sizeField = new JSpinner();
                dimensionPanel.add(sizeField);
                sizeField.setValue(mainFrame.env.floor.width);
                sizeField.setPreferredSize(new Dimension(40, 30));
                sizeField.addChangeListener(new ChangeListener()
                {

                    @Override
                    public void stateChanged(ChangeEvent arg0)
                    {
                        int size = (Integer) sizeField.getValue();
                        if(size < min_dim )
                        {
                            size = min_dim;
                            sizeField.setValue(min_dim);
                            JOptionPane.showMessageDialog(null,"Minimun allowed size is " + min_dim, "Warning", JOptionPane.WARNING_MESSAGE);
                        }
                        if(size > max_dim )
                        {
                            size = max_dim;
                            sizeField.setValue(max_dim);
                            JOptionPane.showMessageDialog(null,"Maximun allowed size is " + max_dim, "Warning", JOptionPane.WARNING_MESSAGE);
                        }
                        mainFrame.env.floor = new Floor(size);
                        //mainFrame.env.floor.initialDirt = 0;
                        System.out.println("env floor al cambiar" + mainFrame.env.floor.width + " " + mainFrame.env.floor.length);
                        mainFrame.getContentPane().remove(mainFrame.gridPanel);
                        mainFrame.gridPanel = new GridPanel(mainFrame.env);
                        mainFrame.getContentPane().add(mainFrame.gridPanel, BorderLayout.EAST);
                        mainFrame.pack();
                    }
                });
                room1= new JLabel("Room 1");
                room2= new JLabel("Room 2");
                room3= new JLabel("Room 3");
                room4= new JLabel("Room 4");

                floorTypeVector.add(Floor.roomType.BEDROOM);
                floorTypeVector.add(Floor.roomType.KITCHEN);
                floorTypeVector.add(Floor.roomType.HALL);
                floorTypeVector.add(Floor.roomType.LIVINGROOM);

                roomTypeCombobox1 = new JComboBox(floorTypeVector);
                roomTypeCombobox1.setSelectedItem(null);


                //floorTypeCombobox.setSelectedItem(mainFrame.env.floor.room.roomType[selectedRoom]);
                roomTypeCombobox1.addItemListener(new ItemListener()
                {

                    @Override
                    public void itemStateChanged(ItemEvent arg0)
                    {


                        mainFrame.env.floor.room[0]= (Floor.roomType)roomTypeCombobox1.getSelectedItem();

                        floorTypeVector.remove(roomTypeCombobox1.getSelectedItem());
                        selectedRoom++;

                        //mainFrame.env.floor.initialDirt = 0;
                        mainFrame.getContentPane().remove(mainFrame.gridPanel);
                        mainFrame.gridPanel = new GridPanel(mainFrame.env);
                        mainFrame.getContentPane().add(mainFrame.gridPanel, BorderLayout.EAST);
                        mainFrame.pack();
                        update();
                    }
                });
                roomTypeCombobox2 = new JComboBox(floorTypeVector);
                roomTypeCombobox2.setSelectedItem(null);

                roomTypeCombobox2.addItemListener(new ItemListener()
                {

                    @Override
                    public void itemStateChanged(ItemEvent arg0)
                    {
                        mainFrame.env.floor.room[1]= (Floor.roomType)roomTypeCombobox2.getSelectedItem();
                        floorTypeVector.remove(roomTypeCombobox2.getSelectedItem());

                        selectedRoom++;
                        mainFrame.getContentPane().remove(mainFrame.gridPanel);
                        mainFrame.gridPanel = new GridPanel(mainFrame.env);
                        mainFrame.getContentPane().add(mainFrame.gridPanel, BorderLayout.EAST);
                        mainFrame.pack();
                        update();
                    }
                });
                roomTypeCombobox3 = new JComboBox(floorTypeVector);
                roomTypeCombobox3.setSelectedItem(null);


                roomTypeCombobox3.addItemListener(new ItemListener()
                {

                    @Override
                    public void itemStateChanged(ItemEvent arg0)
                    {
                        mainFrame.env.floor.room[2]= (Floor.roomType)roomTypeCombobox3.getSelectedItem();

                        floorTypeVector.remove(roomTypeCombobox3.getSelectedItem());
                        selectedRoom++;
                        mainFrame.getContentPane().remove(mainFrame.gridPanel);
                        mainFrame.gridPanel = new GridPanel(mainFrame.env);
                        mainFrame.getContentPane().add(mainFrame.gridPanel, BorderLayout.EAST);
                        mainFrame.pack();
                        update();
                    }
                });
                roomTypeCombobox4 = new JComboBox(floorTypeVector);
                roomTypeCombobox4.setSelectedItem(null);

                roomTypeCombobox4.addItemListener(new ItemListener()
                {

                    @Override
                    public void itemStateChanged(ItemEvent arg0)
                    {
                        mainFrame.env.floor.room[3]= (Floor.roomType)roomTypeCombobox4.getSelectedItem();

                        floorTypeVector.remove(roomTypeCombobox4.getSelectedItem());
                        System.out.println("stanze selezionata"+selectedRoom+ "stanze rimanenti"+floorTypeVector.size());
                        selectedRoom++;
                        mainFrame.getContentPane().remove(mainFrame.gridPanel);
                        mainFrame.gridPanel = new GridPanel(mainFrame.env);
                        mainFrame.getContentPane().add(mainFrame.gridPanel, BorderLayout.EAST);
                        mainFrame.pack();
                        update();
                    }
                });

                randomButton = new JButton("Generate");
                randomButton.addActionListener(new ActionListener()
                {
                    @Override
                    public void actionPerformed(ActionEvent arg0)
                    {

                        //mainFrame.env.floor.clear();
                        //remove(mainFrame.gridPanel);

                        /*si genera una posizione aleatoria per il agente*/
                        Coord agentpos= mainFrame.env.floor.generateObject((Integer) sizeField.getValue());

                        mainFrame.agent.x= agentpos.getX();
                        mainFrame.agent.y= agentpos.getY();

                        roomTypeCombobox1.setSelectedItem(mainFrame.env.floor.room[0]);
                        roomTypeCombobox2.setSelectedItem(mainFrame.env.floor.room[1]);
                        roomTypeCombobox3.setSelectedItem(mainFrame.env.floor.room[2]);
                        roomTypeCombobox4.setSelectedItem(mainFrame.env.floor.room[3]);
                        mainFrame.gridPanel.update();
                        generated= true;
                    }
                });



                dimensionPanel.add(room1);
                dimensionPanel.add(roomTypeCombobox1);

                dimensionPanel.add(room2);
                dimensionPanel.add(roomTypeCombobox2);

                dimensionPanel.add(room3);
                dimensionPanel.add(roomTypeCombobox3);

                dimensionPanel.add(room4);
                dimensionPanel.add(roomTypeCombobox4);
                dimensionPanel.add(randomButton);

            }





            {
                commandPanel = new JPanel();
                commandPanel.setPreferredSize(new Dimension(300,200));
                Border marginOutside = new EmptyBorder(1,10,1,10);
                TitledBorder title = BorderFactory.createTitledBorder("Commands");
                CompoundBorder upperBorder = new CompoundBorder(marginOutside, title);
                Border marginInside = new EmptyBorder(2,10,2,10);


                commandPanel.setBorder(new CompoundBorder(upperBorder, marginInside));

                add(commandPanel, new GridBagConstraints(0, 2, 3, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
                {

                    actionPanel = new JPanel();
                    actionPanel.setLayout(new FlowLayout());

                    /*Start simulation of agent*/
                    resetButton = new JButton();
                    actionPanel.add(resetButton);
                    resetButton.setText("Reset");
                    resetButton.setPreferredSize(new Dimension(75,40));
                    resetButton.addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            int size = (Integer) sizeField.getValue();
                            int steps = (Integer) stepField.getValue();
                            Environment.Type envType =  (Type) envTypeCombobox.getSelectedItem();
                            System.out.println("tipo" +envType.toString());
                            generated =false;
                            mainFrame.agent.x = -1;
                            mainFrame.agent.y = -1;
                            mainFrame.agent.goalReached = false;
                            mainFrame.agent.clearSubGoals();
                            mainFrame.env.floor= new Floor(size);
                            mainFrame.newConfig(size, envType, steps,(Integer) kField.getValue(), generated);
                            mainFrame.stopped=true;
                            controlButton.setIcon(playIcon);

                            mainFrame.gridPanel.update();
                            update();
                            mainFrame.pack();
                            jProgressBar1.setValue(0);
                            roomTypeCombobox1.setSelectedItem(null);
                            roomTypeCombobox2.setSelectedItem(null);
                            roomTypeCombobox3.setSelectedItem(null);
                            roomTypeCombobox4.setSelectedItem(null);

                            randomButton.setEnabled(true);
                        }
                    });

                    /*Start simulation of agent*/
                    controlButton = new JButton();
                    actionPanel.add(controlButton);
                    controlButton.setIcon(playIcon);
                    controlButton.addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            System.out.println("Clicked " + mainFrame.stopped);
                            mainFrame.gridPanel.initialPos();




                            if(mainFrame.stopped && mainFrame.gridPanel.isComplete()) // cambio !!
                            {
                                mainFrame.stopped = false;
                                System.out.println("Start thread");
                                System.out.println("ENV settings" + mainFrame.env.floor.length+ " " +mainFrame.env.floor.width );
                              
                                randomButton.setEnabled(false);
                                class myThread implements Runnable
                                {
                                    public void run()
                                    {
                                        mainFrame.mainLoop(speed);



                                    }
                                }

                                new Thread(new myThread()).start();


                            }
                            else if (mainFrame.gridPanel.isComplete()==false)
                            {

                                JOptionPane.showMessageDialog(null,mainFrame.gridPanel.errorMessage(), "Not all the objects have been assigned to the rooms",
                                                              JOptionPane.ERROR_MESSAGE );

                            }
                            else
                            {

                                System.out.print("stoped true");
                                mainFrame.stopped = true;
                                mainFrame.gridPanel.update();
                               // refreshButton.setEnabled(true);

                            }
                            update();

                        }
                    });
                    /*Start simulation of agent*/
                    onesButton = new JButton();
                    actionPanel.add(onesButton);
                    onesButton.setIcon(nextIcon);
                    onesButton.addActionListener(new ActionListener()
                    {
                        @Override
                        public void actionPerformed(ActionEvent arg0)
                        {
                            class myThread implements Runnable
                            {
                                public void run()
                                {
                                    mainFrame.mainLoopOnes();
                                    try
                                    {
                                        Thread.sleep(1000);
                                        System.out.println("wait");
                                    }
                                    catch (InterruptedException e)
                                    {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }

                                }
                            }
                            
                            new Thread(new myThread()).start();

                        }
                    });
                }
                commandPanel.add(actionPanel);
                progressBarLabel = new JLabel("Remaining Time");
                jProgressBar1= new JProgressBar(0, 100);
                jProgressBar1.setSize(100, 10);
                jProgressBar1.setValue(0);
                jProgressBar1.setStringPainted(true);
                jProgressBar1.setForeground(Color.BLUE);

                final int FPS_MIN = 0;
                final int FPS_MAX = 30;
                final int FPS_INIT = 15;    //initial frames per second
                jSlider = new JSlider(JSlider.HORIZONTAL,
                                      FPS_MIN, FPS_MAX, FPS_INIT);
                jSlider.setValue(1);
                jSliderLabel = new JLabel("Speed");
                jSlider.setMajorTickSpacing(15);

                //Create the label table
                Hashtable labelTable = new Hashtable();

                labelTable.put( new Integer( 5), new JLabel("Slow") );
                labelTable.put( new Integer( 15 ), new JLabel("Medium") );
                labelTable.put( new Integer( FPS_MAX ), new JLabel("Fast") );
                jSlider.setLabelTable( labelTable );


                jSlider.setPaintTicks(true);
                jSlider.setPaintLabels(true);
                jSlider.setLabelTable(labelTable);



                jSlider.addChangeListener(new ChangeListener()
                {
                    public void stateChanged(ChangeEvent e)
                    {
                        speed = jSlider.getValue();
                        System.out.println("Slider2: " + jSlider.getValue());
                    }
                });
                commandPanel.add(jSliderLabel, BorderLayout.CENTER);
                commandPanel.add(jSlider);

                commandPanel.add(progressBarLabel);
                commandPanel.add(jProgressBar1);

            }
            {
                statusPanel = new JPanel();
                Border marginOutside = new EmptyBorder(1,10,1,10);
                TitledBorder title = BorderFactory.createTitledBorder("Status");
                CompoundBorder upperBorder = new CompoundBorder(marginOutside, title);
                Border marginInside = new EmptyBorder(1,10,1,10);
                statusPanel.setBorder(new CompoundBorder(upperBorder, marginInside));
                statusPanel.setPreferredSize(new Dimension(300,250));
                statusPanel.setLayout(new GridLayout(6,1));
                add(statusPanel, new GridBagConstraints(0, 4, 1, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));

                JPanel pmPanel = new JPanel();
                JPanel currTypeEnvPanel = new JPanel();
                statusPanel.add(currTypeEnvPanel);
                {
                    currTypeEnvLabel = new JLabel("Environment's type:");
                    currTypeEnvValueLabel = new JLabel("" + mainFrame.env.type + " !" + mainFrame.env.floor.width);
                    currTypeEnvPanel.add(currTypeEnvLabel);
                    currTypeEnvPanel.add(currTypeEnvValueLabel);
                }
                statusPanel.add(pmPanel);
                {
                    pmLabel = new JLabel("Performance Measure:");
                    pmValueLabel = new JLabel(" "+mainFrame.env.performanceMeasure());
                    pmPanel.add(pmLabel);
                    pmPanel.add(pmValueLabel);
                }
                JPanel stepPanel = new JPanel();
                statusPanel.add(stepPanel);
                {
                    stepsLabel = new JLabel("Steps Number:");
                    stepsValueLabel = new JLabel("" + mainFrame.agent.actionList.size());
                    stepPanel.add(stepsLabel);
                    stepPanel.add(stepsValueLabel);
                }
                subGoalsLabel = new JLabel("Subgoals:");
                statusPanel.add(subGoalsLabel);

                JPanel subGoalsPanel = new JPanel();
                GridLayout layout2 = new GridLayout(3,2);
                subGoalsPanel.setLayout(layout2);
                subGoalsPanel.setPreferredSize(new Dimension(300,400));
                statusPanel.add(subGoalsPanel);
                {

                    subGoal1Label = new JLabel("Meal Ready");//mainFrame.agent.actionList.size()));
                    subGoal2Label = new JLabel("Suit Up");
                    subGoal3Label = new JLabel("Table Ready");
                    subGoal4Label = new JLabel("Flowers Taken");
                    subGoal5Label = new JLabel("Entrance");

                    subGoalsPanel.add(subGoal1Label);
                    subGoalsPanel.add(subGoal2Label);
                    subGoalsPanel.add(subGoal3Label);
                    subGoalsPanel.add(subGoal4Label);
                    subGoalsPanel.add(subGoal5Label);
                }
                /*JPanel currVisibilityPanel = new JPanel();
                statusPanel.add(currVisibilityPanel);
                {
                	currVisibilityLabel = new JLabel("Agent's visibility:");
                	currVisibilityValueLabel = new JLabel("" + mainFrame.agent.visType);
                	currVisibilityPanel.add(currVisibilityLabel);
                	currVisibilityPanel.add(currVisibilityValueLabel);
                }*/
                JPanel goalPanel = new JPanel();
                statusPanel.add(goalPanel);
                {
                    goalReachedLabel = new JLabel("GOAL NOT REACHED");
                    goalReachedLabel.setForeground(Color.RED);
                    goalPanel.add(goalReachedLabel);
                }
            }
        }
    }








    protected void refreshConfi()
    {
        int size = (Integer) sizeField.getValue();
        int steps = (Integer) stepField.getValue();
        Environment.Type envType =  (Type) envTypeCombobox.getSelectedItem();

        if(size < min_dim )
        {
            size = min_dim;
            sizeField.setValue(min_dim);
            JOptionPane.showMessageDialog(null,"Minimun allowed size is " + min_dim, "Warning", JOptionPane.WARNING_MESSAGE);
        }
        if(size > max_dim )
        {
            size = max_dim;
            sizeField.setValue(max_dim);
            JOptionPane.showMessageDialog(null,"Maximun allowed size is " + max_dim, "Warning", JOptionPane.WARNING_MESSAGE);
        }
        this.mainFrame.env.type = (Type) envTypeCombobox.getSelectedItem();
        mainFrame.newConfig(size, envType, steps,(Integer) kField.getValue(), generated);

        update();
        //refreshButton.setText("Refresh");


    }

    public int getStepValue()
    {
        return (Integer) stepField.getValue();
    }

    public void update()
    {
        if(mainFrame.stopped)
            controlButton.setIcon(playIcon);
        else
            controlButton.setIcon(stopIcon);

        currTypeEnvValueLabel.setText("" + mainFrame.env.type);
        pmValueLabel.setText("" + mainFrame.env.performanceMeasure());
        stepsValueLabel.setText("" + mainFrame.agent.actionList.size());
        System.out.println("action list" + mainFrame.agent.actionList.size() +"energy"+ mainFrame.agent.energy);

        if(mainFrame.agent.x != -1 && mainFrame.agent.y != -1)
            jProgressBar1.setValue(mainFrame.agent.energy*(100/getStepValue()));

        if(mainFrame.agent.actionList.size()==0)
            jProgressBar1.setValue(0);

        if(mainFrame.agent.subGoal1Reached())
        {

            subGoal1Label.setForeground(Color.GREEN);
        }
        else
            subGoal1Label.setForeground(Color.black);

        if(mainFrame.agent.subGoal2Reached())
        {

            subGoal2Label.setForeground(Color.GREEN);
        }
        else
            subGoal2Label.setForeground(Color.black);

        if(mainFrame.agent.subGoal3Reached())
        {

            subGoal3Label.setForeground(Color.GREEN);
        }
        else
            subGoal3Label.setForeground(Color.black);

        if(mainFrame.agent.subGoal4Reached())
        {

            subGoal4Label.setForeground(Color.GREEN);
        }
        else
            subGoal4Label.setForeground(Color.black);

        if(mainFrame.agent.subGoal5Reached())
        {

            subGoal5Label.setForeground(Color.GREEN);
        }
        else
            subGoal5Label.setForeground(Color.black);

        if(mainFrame.agent.goalReached)
        {
            goalReachedLabel.setForeground(Color.GREEN);
            goalReachedLabel.setText("GOAL REACHED");
        }
        else
        {
            goalReachedLabel.setForeground(Color.RED);
            goalReachedLabel.setText("GOAL NOT REACHED");
        }
    }

}
